var ns, app,toast, actionsheet,items,WXPhotoPicker;
app = new Vue({
    el: '#app',
    data:{
        UserName:'',
        startTime:'',
        endTime:'',
        form:{
            ID:'',
            Name:'',
            PhoneNumber:'',
            WorkTime:'',
            Desc:'',
            ImagePath:'',
            QRCodeImgPath:'',
        },
        maxuploadcount: 1,
        txtVal: 0,
    },
    methods:{
        get_data: function() {
            var that = this;
            var options = {};           
            options.action = "getbuildingmemberdetail";
            options.ID=that.form.ID;
            ns.post(options, function(succeed, data, err) {
                if (succeed) {                   
                    that.form = data.data;
                    that.startTime=that.form.WorkTime.split('-')[0];
                    that.endTime=that.form.WorkTime.split('-')[1];
                    // that.form.QRCodePath=that.form.QRCodeImgPath;
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
                that.initPicker();
            }, {
                toast: true
            });
        },
        do_start_time: function() {
            var that = this;
            api.openPicker({
                type: 'time',
                title: '选择时间'
            }, function(ret, err) {
                if (ret) {
                    var hour = (ret.hour >= 10 ? ret.hour : '0' + ret.hour);
                    var minute = (ret.minute >= 10 ? ret.minute : '0' + ret.minute);
                    that.startTime =hour + ':' + minute;
                }
            });
        },
        do_end_time: function() {
            var that = this;
            api.openPicker({
                type: 'time',
                title: '选择时间'
            }, function(ret, err) {
                if (ret) {
                    var hour = (ret.hour >= 10 ? ret.hour : '0' + ret.hour);
                    var minute = (ret.minute >= 10 ? ret.minute : '0' + ret.minute);
                    that.endTime =hour + ':' + minute;
                }
            });
        },

        do_save:function(){
            var that=this;
            
            if (that.form.Name == "") {
                api.toast({
                    msg: '请填写姓名',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            if (that.form.PhoneNumber == "") {
                api.toast({
                    msg: '请填写电话号码',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            that.form.WorkTime=that.startTime+'-'+that.endTime;
            ns.post({
                action: 'savebuildingmember',
                ID:that.form.ID,
                Name:that.form.Name,
                PhoneNumber:that.form.PhoneNumber,
                WorkTime:that.form.WorkTime,              
                Desc:that.form.Desc,
                ImagePath:that.form.ImagePath,
                QRCodePath:that.form.QRCodeImgPath,
                }, function(succeed, data, err) {
                if (succeed) {
                    api.toast({
                        msg: '提交成功',
                        duration: 2000,
                        location: 'bottom'
                    });
                    api.sendEvent({                      
                        name: 'home_new_updata',
                        extra: {
                            key1: 'value1',
                        }
                    });
                    setTimeout(function() {
                        api.closeWin();
                    }, 1000);
                } else if (err) {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            },{
                files: {
                    "photo":that.form.ImagePath,
                    "qrcode":that.form.QRCodeImgPath
                }
            });
        },

        choose_picture: function(item) {
            var that = this;
           that.items=item;
            ns.confirmPer('camera', function() {
                that.choose_picture_start();           
            });
        },

        choose_picture_start:function(){
            var that = this;
            if (that.imglist.length >= that.maxuploadcount) {
                api.toast({
                    msg: '最多支持' + that.maxuploadcount + '张图片',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            api.actionSheet({
                cancelTitle: '取消',
                buttons: ['拍照', '从相册选择']
            }, function(ret, err) {
                //拍照
                if(ret.buttonIndex==1){ 
                    that.upload_choose_picture();
                    return;
                }
                //从相册选择
                if(ret.buttonIndex==2){
                    that.choose_picture_album();
                    return;
                } 
                //取消 
                else{
                    return false;  
                }  
            });
        },
        upload_choose_picture:function(){
            var that = this;
            api.getPicture({
                sourceType: 'camera',
                encodingType: 'jpg',
                destinationType: 'url',
                allowEdit: false,
                saveToPhotoAlbum: true,
            }, function(ret, err) {
                if (ret&&ret.data) {
                    if(that.items==1){
                        that.form.ImagePath= ret.data;
                    }
                    if(that.items==2){
                        that.form.QRCodeImgPath= ret.data;
                    }
                } 
            });
        },
        choose_picture_album:function(){
            var that = this;
            WXPhotoPicker.open({
                max: 1,
                type:'image',
            }, function(ret) {
                if (ret) {
                    if(ret.list.length>0){
                        if(that.items==1){
                            that.form.ImagePath= ret.list[0].path;
                        }
                        if(that.items==2){
                            that.form.QRCodeImgPath= ret.list[0].path;
                        }
                    } 
                }
            });
        },
        checkStrLengths: function () {
            var that = this;
            if (that.form.Desc.length <= 50) {
                that.txtVal = that.form.Desc.length;
            }
            else {
                that.txtVal = 50;
                that.form.Desc = that.form.Desc.substring(0, 50);
            }

        },
        initPicker: function () {
            var _this = this;
            (function ($, doc) {
                $.init();
                $.ready(function () {
                    //开始日期
                    var btn = document.querySelector('#startdate');
                    btn.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            if(_this.startTime){
                                _self.picker.setSelectedValue(_this.startTime);//回显
                            }
                            _self.picker.show(function (rs) {
                                _this.startTime = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            if(_this.startTime){
                                _self.picker.setSelectedValue(_this.startTime);//回显
                            }
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.startTime = rs.value;
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);
                    //结束日期
                    var btnEnd = document.querySelector('#enddate');
                    btnEnd.addEventListener('tap', function () {
                        var _self = this;
                        if (_self.picker) {
                            if(_this.endTime){
                                _self.picker.setSelectedValue(_this.endTime);//回显
                            }
                            _self.picker.show(function (rs) {
                                _this.endTime = rs.value;
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        } else {
                            var optionsJson = this.getAttribute('data-options') || '{}';
                            var options = JSON.parse(optionsJson);
                            var id = this.getAttribute('id');
                            _self.picker = new $.DtPicker(options);
                            if(_this.endTime){
                                _self.picker.setSelectedValue(_this.endTime);//回显
                            }
                            _self.picker.show(function (rs) {
                                var timer = setTimeout(function () {
                                    clearTimeout(timer);
                                    _this.endTime = rs.value;
                                }, 300);
                                _self.picker.dispose();
                                _self.picker = null;
                            });
                        }
                    }, false);
                });
            })(mui, document);
        },
    },

});
apiready = function() {
    var id=api.pageParam.id;
    setTimeout(function() {
        app.get_data();
    }, 500)
    WXPhotoPicker = api.require('WXPhotoPicker');
    app.UserName=api.getPrefs({
        sync:true,
        key:'loginname'        
    });
    api.parseTapmode();
    ns = window.Foresight.Util;
};